/**
 * @fileoverview This is a file where undefinedVars checks are disabled.
 * @suppress {undefinedVars}
 */
// the above is for the Google Closure Compiler ant task


var ships_map = {}; // hashmap for holding ships information
for (var i=0; i<=10; i++)
{
	if (i<=3)
		{ 
		ships_map[i]= {size:1, location:[],locatedInTable:0, position:0};
		}
	if (i<=6 && i>3)
		{
		ships_map[i]= {size:2, location:[] ,locatedInTable:0, position:0 };
		}
	if (i<=8 && i>6)
		{
		ships_map[i]= {size:3, location:[],locatedInTable:0, position:0};
		}
	if (i==9)
		{
		ships_map[i]= {size:4, location:[],locatedInTable:0, position:0};
		}

}

var ships_locations= [];


function allowDrop(ev)
{
		ev.preventDefault();

}

var myID= null;
var myData = { "gameID" : "22",
        "userID" : 2,
        "userShipList" : [[[-1,-1], [-1,-1]]] //Real data are inserted when three dimensional ships_locations is created
    };

//function servlettest(){
//	
//	jQuery.ajax({
//        url : "UserShipServlet",
//        data: {"data": JSON.stringify(myData)},
//        dataType: "text",
//		success: function(data){
//		alert(data);
//		buff.userID=data;
//		console.log(buff.userID);
//		},
//
//        error : function(data) {
//            console.log("Error: ", data);
//        }  ,
//        type : "post",
//        timeout : 30000
//    });
//}
//	
//
//function servlettest2(){
//		
//	$.ajax({
//		type: 'GET',
//		url: "helloworld",
//		dataType: "text",
//		success: function(data){
//			alert(data);
//			//myData.gameID= data;
//			//console.log(myData.gameID);
//		}
//	});
//}


function drag(ev)
{
ev.dataTransfer.setData("Text",ev.target.id);
}



function drop(ev)
{
//	testing();
//	testing2();
//	testing3();
	var data=ev.dataTransfer.getData("Text"); //ship ID
	
	// Do make it impossible to put one ship on top of another
	if (ev.target.parentNode.rowIndex === undefined || ev.target.cellIndex === undefined){
		ev.preventDefault();
		return false;
	}
	
	
	var shipinfo= ships_map[data[0]];
	var holdCurrentShipLocationData = shipinfo.location.slice();
	shipinfo.location=[]; //empty location info
	//console.log(holdCurrentShipLocationData);
	console.log(ev.target);
	var arrayForCheckingRows= [-1,0,1];
	// Adding ship location info to shipinfo
	for (var i=0; i<shipinfo.size; i++)
	{	
	if (shipinfo.position === 0){
//	if (true){
	if (currentShipLocations()=== true){
		for (var ii=0; ii<ships_locations.length;ii++){
			for (var jj=0; jj<ships_locations[ii].length; jj++){
				for (var jjj=0; jjj<arrayForCheckingRows.length; jjj++){
				if ( ships_locations[ii][jj][0] === (ev.target.parentNode.rowIndex + arrayForCheckingRows[jjj]) ) {
					
					if (ships_locations[ii][jj][1]===ev.target.cellIndex+i){
						ev.preventDefault();
						shipinfo.location=holdCurrentShipLocationData;
						return false; 
					}
					if (ships_locations[ii][jj][1]===ev.target.cellIndex-1){
						ev.preventDefault();
						shipinfo.location=holdCurrentShipLocationData.slice();
						return false; 
					}
					if (ships_locations[ii][jj][1]===ev.target.cellIndex+i+1){
						ev.preventDefault();
						shipinfo.location=holdCurrentShipLocationData;
						return false; 
					}	
				}
				}
				

		}
		}
		}
	}
	if (shipinfo.position === 1){ //this will handle rotated ship table check
		
		if (currentShipLocations()=== true){
			for (var ii=0; ii<ships_locations.length;ii++){
				for (var jj=0; jj<ships_locations[ii].length; jj++){
					for (var jjj=0; jjj<arrayForCheckingRows.length; jjj++){
					if ( ships_locations[ii][jj][0] === (ev.target.parentNode.rowIndex +(i) + arrayForCheckingRows[jjj]) ) {
						
						if (ships_locations[ii][jj][1]===ev.target.cellIndex){
							ev.preventDefault();
							shipinfo.location=holdCurrentShipLocationData;
							return false; 
						}
						if (ships_locations[ii][jj][1]===ev.target.cellIndex-1){
							ev.preventDefault();
							shipinfo.location=holdCurrentShipLocationData.slice();
							return false; 
						}
						if (ships_locations[ii][jj][1]===ev.target.cellIndex+1){
							ev.preventDefault();
							shipinfo.location=holdCurrentShipLocationData;
							return false; 
						}	
					}
					}
					

			}
			}
			}
		}

	}
	

for (var i=0; i<shipinfo.size; i++)
	{

		if (shipinfo.position === 0){	
			if ((ev.target.cellIndex+i)> 10  || ((ev.target.cellIndex+i)<=0 ))  { // if ship is added out of table, then cancel
				ev.preventDefault();
				shipinfo.location=holdCurrentShipLocationData;
				return false;
			}
			shipinfo.location.push([]);
			shipinfo.location[i].push(ev.target.parentNode.rowIndex);
			shipinfo.location[i].push(ev.target.cellIndex+i);
			}
		if (shipinfo.position === 1){
			if ((ev.target.parentNode.rowIndex+i)> 10  || ((ev.target.parentNode.rowIndex+i)<=0 ))  { // if ship is added out of table, then cancel
				ev.preventDefault();
				shipinfo.location=holdCurrentShipLocationData;
				return false;
			}
			shipinfo.location.push([]);
			shipinfo.location[i].push(ev.target.parentNode.rowIndex+i);
			shipinfo.location[i].push(ev.target.cellIndex);
			}
			
				
	}

	shipinfo.locatedInTable = 1;
	console.log(ships_locations);
	currentShipLocations();
	console.log(ships_locations);
	console.log(ships_locations.length);
	ev.target.appendChild(document.getElementById(data));
	ev.preventDefault();
console.log(ships_locations[0][0][0]);
//console.log(ships_map);
}


function currentShipLocations(){
	ships_locations= [];
	for (var i=0; i<10; i++)
	{

		if (ships_map[i].locatedInTable === 0){
		}
		else {
			ships_locations.push(ships_map[i].location);
		}
	}
	myData.userShipList=ships_locations;
	return true;
	
}

function checkIfAllShipsHaveBeenLocated(){
	ships_locations= [];
	for (var i=0; i<10; i++)
	{

		if (ships_map[i].locatedInTable === 0){
			console.log("K�ik laevad pole paigutatud");
			return false;
		}
		else {
			ships_locations.push(ships_map[i].location);
		}
	}
	


	return ships_locations;
	
	
}


